//
//  LGPersionalDataController.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/2/14.
//  个人资料

#import "LGPersionalDataController.h"
//编辑
#import "LGEditPersonalDataController.h"
#import "LGPersonalTitleView.h"
#define lineHeight   viewPix(44)

@interface LGPersionalDataController ()<UIScrollViewDelegate>
/**   */
@property (nonatomic , strong)UILabel *titleLabel;
/**   */
@property (nonatomic , strong)UIButton *editBtn;
/**   */
@property (nonatomic , strong)UIScrollView *scrollView;
/**   */
@property (nonatomic , strong)UIImageView *headPic;
/**   */
@property (nonatomic , strong)UILabel *nameLabel;
/**   */
@property (nonatomic , strong)UIView *baseView;

/**   */
@property (nonatomic , strong)LGPersonalTitleView *hospitalView;
/**   */
@property (nonatomic , strong)LGPersonalTitleView *departmentView;
/**   */
@property (nonatomic , strong)LGPersonalTitleView *posttitleView;
/**   */
@property (nonatomic , strong)LGPersonalTitleView *introduceView;
/**   */
@property (nonatomic , strong)UILabel *introduceLabel;
/**   */
@property (nonatomic , strong)LGPersonalTitleView *goodView;
/**   */
@property (nonatomic , strong)UILabel *goodLabel;

/**   */
@property (nonatomic , strong)NSDictionary *infoDic;

@end

@implementation LGPersionalDataController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self createSubView];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear: animated];
    [self requestData];
}

-(void)setUserId:(NSString *)userId{
    _userId = userId;
    if([userId isEqualToString:kUserId]){
        //自己 -- >可以编辑
        [self.view addSubview:self.editBtn];
        [self navigationViewWithType:LGNavigationTypeDark title:@"个人资料"];
        [self.navigationView.rightBtn setTitle:@"编辑" forState:UIControlStateNormal];
        [self.navigationView.rightBtn setTitleColor:[UIColor colorWithString:@"#333333"] forState:UIControlStateNormal];
        self.navigationView.rightBtn.titleLabel.font = LGFontWeight(16, UIFontWeightMedium);
        self.navigationView.rightBtn.frame = CGRectMake(Screen_W-viewPix(56), statusBarHeight, viewPix(40), 44);
        self.navigationView.rightBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
        [self.navigationView.rightBtn addTarget:self action:@selector(editButtonAction) forControlEvents:UIControlEventTouchUpInside];
        
    }else{
        //其他医生 -- >不可编辑
        
    }
}

-(void)requestData{
    [RequestUtil withPOST:@"/doctor/my.profile/detail" parameters:@{@"doctor_id":self.userId} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
        if (code == 200) {
            NSDictionary *tempDic = returnData;
            self.infoDic = tempDic;
            [self.headPic sd_setImageWithURL:[NSURL URLWithString:tempDic[@"avatar_url"]] placeholderImage:defaultDoctorPic];
            self.nameLabel.text = LGNSString(tempDic[@"realname"]);
            self.hospitalView.content = LGNSString(tempDic[@"hospital"]);
            self.departmentView.content = LGNSString(tempDic[@"department"]);
            self.posttitleView.content = LGNSString(tempDic[@"profess"]);
            self.introduceLabel.text = LGNSString(tempDic[@"intro"]);
            self.goodLabel.text = LGNSString(tempDic[@"adeptat"]);
            [self resetContentSize:LGNSString(tempDic[@"intro"]) good:LGNSString(tempDic[@"adeptat"])];
        }else{
            [TooltipView showMessage:message offset:0];
        }
    } error:^(NSError *error) {
        [TooltipView showMessage:LGErrorMessage offset:0];
    }];
}
//编辑
-(void)editButtonAction{
    LGEditPersonalDataController *controller = [[LGEditPersonalDataController alloc]init];
    controller.infoDic = self.infoDic;
    [self.navigationController pushViewController:controller animated:YES];
}

-(void)resetContentSize:(NSString *)introduce good:(NSString *)goodStr{
    CGFloat introduce_H = [introduce textHeightWithFont:LGFont(16) maxWidth:Screen_W-viewPix(52)];
    CGFloat goodStr_H = [goodStr textHeightWithFont:LGFont(16) maxWidth:Screen_W-viewPix(52)];
    CGFloat height = introduce_H+goodStr_H+viewPix(458)+viewPix(50);
    height = (height>Screen_H)?height:(Screen_H+2);
    self.scrollView.contentSize = CGSizeMake(Screen_W, height);
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat offset_Y = scrollView.contentOffset.y;
    CGFloat percent = (CGFloat)offset_Y/viewPix(60);
    self.navigationView.alpha = percent;
    self.backBtn.alpha = 1-percent;
    self.titleLabel.alpha = 1-percent;
    self.editBtn.alpha = 1-percent;
    [self setNeedsStatusBarAppearanceUpdate];
}

-(UIStatusBarStyle)preferredStatusBarStyle{
    CGFloat offset_Y = self.scrollView.contentOffset.y;
    if(offset_Y>=viewPix(40)){
        return UIStatusBarStyleDefault;
    }
    return UIStatusBarStyleLightContent;
}

#pragma mark
#pragma mark ====> 创建控件
-(void)createSubView{
    self.fd_prefersNavigationBarHidden = YES;
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.scrollView];
    [self.view addSubview:self.backBtn];
    [self.view addSubview:self.titleLabel];
    
    [self.backBtn setImage:kNavBackImageWhite forState:UIControlStateNormal];
    
    self.navigationView.backgroundColor = [UIColor whiteColor];
    self.navigationView.alpha = 0;
    [self.scrollView addSubview:self.headPic];
    [self.scrollView addSubview:self.nameLabel];
    [self.scrollView addSubview:self.baseView];
    [self.baseView addSubview:self.hospitalView];
    [self.baseView addSubview:self.departmentView];
    [self.baseView addSubview:self.posttitleView];
    [self.baseView addSubview:self.introduceView];
    [self.baseView addSubview:self.introduceLabel];
    [self.baseView addSubview:self.goodView];
    [self.baseView addSubview:self.goodLabel];
    [self.introduceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.introduceView.mas_bottom);
        make.left.equalTo(self.view).offset(viewPix(36));
        make.right.equalTo(self.view).offset(-viewPix(16));
    }];
    [self.goodView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.introduceLabel.mas_bottom).offset(viewPix(5));
        make.left.right.equalTo(self.view);
        make.height.equalTo(@(lineHeight));
    }];
    [self.goodLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.goodView.mas_bottom);
        make.left.equalTo(self.view).offset(viewPix(36));
        make.right.equalTo(self.view).offset(-viewPix(16));
    }];
}

-(UILabel *)titleLabel{
    if(!_titleLabel){
        _titleLabel = [UILabel lableWithText:@"个人资料" colorString:@"#FFFFFF" textFont:kNavTitleFont textAlignment:NSTextAlignmentCenter lines:1];
        _titleLabel.frame = CGRectMake(viewPix(100), statusBarHeight, Screen_W-viewPix(200), 44);
    }
    return _titleLabel;
}

-(UIButton *)editBtn{
    if(!_editBtn){
        _editBtn = [UIButton buttonWithTitle:@"编辑" titleFont:LGFontWeight(16, UIFontWeightMedium) textColor:@"#FFFFFF" imageName:@""];
        _editBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
        _editBtn.frame = CGRectMake(Screen_W-viewPix(56), statusBarHeight, viewPix(40), 44);
        [_editBtn addTarget:self action:@selector(editButtonAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _editBtn;
}

-(UIScrollView *)scrollView{
    if(!_scrollView){
        _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, Screen_W, Screen_H)];
        _scrollView.backgroundColor = [UIColor colorWithString:@"#2E8FFF"];
        _scrollView.contentSize = CGSizeMake(Screen_W, Screen_H+2);
        _scrollView.showsVerticalScrollIndicator = NO;
        _scrollView.delegate = self;
    }
    return _scrollView;
}

-(UIImageView *)headPic{
    if(!_headPic){
        _headPic = [[UIImageView alloc]initWithImage:defaultDoctorPic];
        _headPic.frame = CGRectMake((Screen_W-viewPix(64))/2.0, topBarHeight+viewPix(16), viewPix(64), viewPix(64));
        _headPic.backgroundColor = [UIColor colorWithString:@"#90C3FA"];
        _headPic.layer.borderColor = [UIColor colorWithString:@"#FFFFFF"].CGColor;
        _headPic.layer.borderWidth = 2;
        _headPic.layer.cornerRadius = viewPix(32);
        _headPic.clipsToBounds = YES;
    }
    return _headPic;
}

-(UILabel *)nameLabel{
    if(!_nameLabel){
        _nameLabel = [UILabel lableWithText:@"姓名" colorString:@"#FFFFFF" textFont:LGFontWeight(16, UIFontWeightMedium) textAlignment:NSTextAlignmentCenter lines:1];
        _nameLabel.frame = CGRectMake(viewPix(16), kMaxY(self.headPic.frame)+viewPix(3), Screen_W-viewPix(32), viewPix(20));
    }
    return _nameLabel;
}

-(UIView *)baseView{
    if(!_baseView){
        _baseView = [[UIView alloc]initWithFrame:CGRectMake(0, topBarHeight+viewPix(120), Screen_W, Screen_H*3)];
        _baseView.backgroundColor = [UIColor whiteColor];
        _baseView.cornerRidus = 15;
    }
    return _baseView;
}

-(LGPersonalTitleView *)hospitalView{
    if(!_hospitalView){
        _hospitalView = [[LGPersonalTitleView alloc] initWithFrame:CGRectMake(0, viewPix(15), Screen_W, lineHeight)];
        [_hospitalView viewWithIcon:@"个人资料-医院" title:@"医院"];
    }
    return _hospitalView;
}

-(LGPersonalTitleView *)departmentView{
    if(!_departmentView){
        _departmentView = [[LGPersonalTitleView alloc] initWithFrame:CGRectMake(0, kMaxY(self.hospitalView.frame), Screen_W, lineHeight)];
        [_departmentView viewWithIcon:@"个人资料-科室" title:@"科室"];
    }
    return _departmentView;
}

-(LGPersonalTitleView *)posttitleView{
    if(!_posttitleView){
        _posttitleView = [[LGPersonalTitleView alloc] initWithFrame:CGRectMake(0, kMaxY(self.departmentView.frame), Screen_W, lineHeight)];
        [_posttitleView viewWithIcon:@"个人资料-职称" title:@"职称"];
    }
    return _posttitleView;
}

-(LGPersonalTitleView *)introduceView{
    if(!_introduceView){
        _introduceView = [[LGPersonalTitleView alloc] initWithFrame:CGRectMake(0, kMaxY(self.posttitleView.frame), Screen_W, lineHeight)];
        [_introduceView viewWithIcon:@"个人资料-个人简介" title:@"个人简介"];
    }
    return _introduceView;
}

-(LGPersonalTitleView *)goodView{
    if(!_goodView){
        _goodView = [[LGPersonalTitleView alloc] init];
        [_goodView viewWithIcon:@"个人资料-擅长领域" title:@"擅长领域"];
    }
    return _goodView;
}

-(UILabel *)introduceLabel{
    if(!_introduceLabel){
        _introduceLabel = [UILabel lableWithText:@"" colorString:@"#666666" textFont:LGFont(16) textAlignment:NSTextAlignmentLeft lines:0];
    }
    return _introduceLabel;
}

-(UILabel *)goodLabel{
    if(!_goodLabel){
        _goodLabel = [UILabel lableWithText:@"" colorString:@"#666666" textFont:LGFont(16) textAlignment:NSTextAlignmentLeft lines:0];
    }
    return _goodLabel;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
